package com.zhy.http.library.utils;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.global.configuration.DeviceCapability;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.InputStream;
import java.lang.reflect.Field;

/**
 * Created by zhy on 15/11/6.
 */
public class ImageUtils {
    /**
     * 根据InputStream获取图片实际的宽度和高度
     *
     * @param imageStream InputStream
     * @return image size
     */
    public static ImageSize getImageSize(InputStream imageStream) {
        ImageSource.DecodingOptions options = new ImageSource.DecodingOptions();
        options.allowPartialImage = true;

        ImageSource.SourceOptions opts = new ImageSource.SourceOptions();
        opts.formatHint = "image/png";
        ImageSource imageSource = ImageSource.create(imageStream, opts);

        ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();
        decodingOpts.desiredSize = new Size(0, 0);
        decodingOpts.desiredRegion = new Rect(0, 0, 0, 0);
        decodingOpts.desiredPixelFormat = PixelFormat.ARGB_8888;

        PixelMap pixelmap = imageSource.createPixelmap(decodingOpts);
        Size size = pixelmap.getImageInfo().size;
        return new ImageSize(size.width, size.height);
    }

    public static class ImageSize {
        int width;
        int height;

        public ImageSize() {}

        public ImageSize(int width, int height) {
            this.width = width;
            this.height = height;
        }

        @Override
        public String toString() {
            return "ImageSize{" + "width=" + width + ", height=" + height + '}';
        }
    }

    public static int calculateInSampleSize(ImageSize srcSize, ImageSize targetSize) {
        int width = srcSize.width;
        int height = srcSize.height;
        int inSampleSize = 1;

        int reqWidth = targetSize.width;
        int reqHeight = targetSize.height;

        if (width > reqWidth && height > reqHeight) {
            int widthRatio = Math.round((float) width / (float) reqWidth);
            int heightRatio = Math.round((float) height / (float) reqHeight);
            inSampleSize = Math.max(widthRatio, heightRatio);
        }
        return inSampleSize;
    }


    public static ImageSize getImageViewSize(Component view) {
        ImageSize imageSize = new ImageSize();

        imageSize.width = getExpectWidth(view);
        imageSize.height = getExpectHeight(view);

        return imageSize;
    }

    private static int getExpectHeight(Component view) {
        int height = 0;
        if (view == null) return 0;

        final ComponentContainer.LayoutConfig params = view.getLayoutConfig();

        if (params != null && params.height != ComponentContainer.LayoutConfig.MATCH_CONTENT) {
            height = view.getWidth();
        }
        if (height <= 0 && params != null) {
            height = params.height;
        }

        if (height <= 0) {
            height = getImageViewFieldValue(view, "mMaxHeight"); // 获得设置的最大的宽度
        }

        if (height <= 0) {
            DeviceCapability displayMetrics = view.getContext().getResourceManager().getDeviceCapability();
            height = displayMetrics.height;
        }

        return height;
    }

    private static int getExpectWidth(Component view) {
        int width = 0;
        if (view == null) return 0;

        final ComponentContainer.LayoutConfig params = view.getLayoutConfig();

        if (params != null && params.width != ComponentContainer.LayoutConfig.MATCH_CONTENT) {
            width = view.getWidth();
        }
        if (width <= 0 && params != null) {
            width = params.width;
        }

        if (width <= 0) {
            width = getImageViewFieldValue(view, "mMaxWidth"); // 获得设置的最大的宽度
        }

        if (width <= 0) {
            DeviceCapability displayMetrics = view.getContext().getResourceManager().getDeviceCapability();
            width = displayMetrics.width;
        }

        return width;
    }

    private static int getImageViewFieldValue(Object object, String fieldName) {
        int value = 0;
        try {
            Field field = Image.class.getDeclaredField(fieldName);
            field.setAccessible(true);
            int fieldValue = field.getInt(object);
            if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) {
                value = fieldValue;
            }
        } catch (Exception e) {
            return value;
        }
        return value;
    }
}
